﻿'
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2010
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports System.Text.RegularExpressions

Namespace DotNetNuke.Modules.Media.Utilities

    Public NotInheritable Class RegExUtility

#Region " Constants "

        Private Const POSITIVE_ONLY_PATTERN As String = "^\d+(\.\d+)*?$"
        Private Const NEGATIVE_ALLOWED_PATTERN As String = "^\-*\d+(\.\d+)*?$"
        Private Const BOOLEAN_PATTERN As String = "^(1|0|true|false)$"

#End Region

        ''' <summary>
        ''' IsNumber - this method uses a regular expression to determine if the value object is in a valid numeric format.
        ''' </summary>
        ''' <param name="Value">Object - the object to parse to see if it's a number</param>
        ''' <returns>If true, the Value object was in a valid numeric format</returns>
        ''' <remarks>
        ''' This method does not consider commas (,) to be a valid character. This overload defaults PositiveOnly to True.
        ''' </remarks>
        ''' <history>
        ''' [wstrohl] - 20100130 - created
        ''' </history>
        Public Overloads Shared Function IsNumber(ByVal Value As Object) As Boolean
            Return IsNumber(Value, True)
        End Function

        ''' <summary>
        ''' IsNumber - this method uses a regular expression to determine if the value object is in a valid numeric format.
        ''' </summary>
        ''' <param name="Value">Object - the object to parse to see if it's a number</param>
        ''' <param name="PositiveOnly">Boolean - if true, a negative number will be considered valid</param>
        ''' <returns>If true, the Value object was in a valid numeric format</returns>
        ''' <remarks>
        ''' This method does not consider commas (,) to be a valid character.
        ''' </remarks>
        ''' <history>
        ''' [wstrohl] - 20100130 - created
        ''' </history>
        Public Overloads Shared Function IsNumber(ByVal Value As Object, ByVal PositiveOnly As Boolean) As Boolean

            If Value Is Nothing Then
                Return False
            End If

            If PositiveOnly Then
                Return Regex.IsMatch(Value.ToString, POSITIVE_ONLY_PATTERN)
            Else
                Return Regex.IsMatch(Value.ToString, NEGATIVE_ALLOWED_PATTERN)
            End If

        End Function

        ''' <summary>
        ''' IsBoolean - this method uses a regular expression to determine if the value object is in a valid boolean format.
        ''' </summary>
        ''' <param name="Value">Object - the object to parse to see if it is in a boolean fomat</param>
        ''' <returns>If true, the Value object was in a valid boolean format</returns>
        ''' <remarks>
        ''' This method looks for one of the following: 1, 0, true, false (case insensitive)
        ''' </remarks>
        ''' <history>
        ''' [wstrohl] - 20100130 - created
        ''' </history>
        Public Overloads Shared Function IsBoolean(ByVal Value As Object) As Boolean

            If Value Is Nothing Then
                Return False
            End If

            Return Regex.IsMatch(Value.ToString, BOOLEAN_PATTERN, RegexOptions.IgnoreCase)

        End Function

    End Class

End Namespace